package com.lzj.admin.controller;


import com.lzj.admin.query.UserQuery;
import com.lzj.admin.model.RespBean;
import com.lzj.admin.pojo.User;
import com.lzj.admin.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lvyuanbo
 * @since 2021-08-09
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService iUserService;

  /*  @RequestMapping("login")
    @ResponseBody
    public RespBean login(String userName, String password, HttpSession session) {

            User user = iUserService.login(userName, password);
            session.setAttribute("user", user);
            return RespBean.success("登陆成功");

    }*/


    //    @RequestMapping("setting")
//    public String setting(HttpSession session){
//        User user = (User) session.getAttribute("user");
//        session.setAttribute("user",iUserService.getById(user.getId()));
//        return "user/setting";
//    }
    @RequestMapping("setting")
    public String setting(Principal principal, Model model) {
        User user = iUserService.findUserByUserName(principal.getName());
        model.addAttribute("user", user);
        return "user/setting";
    }


    /***
     * @Description: 用户信息更新
     * @Author: lvyuanbo
     * @Date: 2021/8/10
     */
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public RespBean updateUserInfo(User user) {

        iUserService.updateUserInfo(user);
        return RespBean.success("用户信息更新成功");

    }

    /**
     * 用户密码更新页
     *
     * @return
     */
    @RequestMapping("password")
    public String password() {
        return "user/password";
    }


//    /**
//     * 用户密码更新
//     * @param session 旧版
//     * @param oldPassword
//     * @param newPassword
//     * @param confirmPassword
//     * @return
//     */
//    @RequestMapping("updateUserPassword")
//    @ResponseBody
//    public RespBean updateUserPassword(HttpSession session,String oldPassword,String newPassword,String confirmPassword){
//
//            User user = (User) session.getAttribute("user");
//            iUserService.updateUserPassword(user.getUsername(),oldPassword,newPassword,confirmPassword);
//            return RespBean.success("用户密码更新成功");
//    }

    /**
     * 用户密码更新---使用框架后处理
     *
     * @param principal
     * @param oldPassword
     * @param newPassword
     * @param confirmPassword
     * @return
     */
    @RequestMapping("updateUserPassword")
    @ResponseBody
    public RespBean updateUserPassword(Principal principal, String oldPassword, String newPassword, String confirmPassword) {


        iUserService.updateUserPassword(principal.getName(), oldPassword, newPassword, confirmPassword);
        return RespBean.success("用户密码更新成功");
    }

    /**
     * 用户管理主页
     *
     * @return
     */
    @RequestMapping("index")
    @PreAuthorize("hasAnyAuthority('1010')")
    public String index() {
        return "user/user";
    }

    /*
    用户列表查询
     */
    @RequestMapping("list")
    @PreAuthorize("hasAnyAuthority('101003')")
    @ResponseBody
    public Map<String, Object> userList(UserQuery userQuery) {

        return iUserService.userList(userQuery);
    }

    /*
        添加|更新用户页
     */
    @RequestMapping("addOrUpdateUserPage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (id != null) {
            model.addAttribute(iUserService.getById(id));
        }
        return "user/add_update";
    }

    /*
            添加用户
         */
    @RequestMapping("save")
    @ResponseBody
    public RespBean saveUser(User user) {
        iUserService.saveUser(user);
        return RespBean.success("用户添加成功");
    }

    /*
                更新用户
             */
    @RequestMapping("update")
    @ResponseBody
    public RespBean update(User user) {
        iUserService.updateUser(user);
        return RespBean.success("用户更新成功");
    }

    /*
    删除
     */
    @ResponseBody
    @RequestMapping("delete")
    public RespBean delete(Integer[] ids){
        iUserService.deleteUser(ids);
        return RespBean.success("用户删除成功");
    }
}
